package com.m3839.sdk.ym.core

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * Description:DateUtils
 *
 * @author 陈宝阳
 * @create 2020/4/14 16: 28
 */
internal class YMDateManager private constructor() {

    private val formatTime = "HH:mm:ss"
    private val sdf = SimpleDateFormat("yyyy-MM-dd")
    private val holidays: MutableList<String> = mutableListOf()
    private val workdays: MutableList<String> = mutableListOf()

    /**
     * 当天什么时候可以开始可以玩
     */
    private var dayStartTime: String? = null

    /**
     * 当天什么时候可以开始不能玩
     */
    private var datEndTime: String? = null

    private object SingletonHolder {
        val INSTANCE = YMDateManager()
    }

    fun initConfig(
        holidays: MutableList<String>?,
        workdays: MutableList<String>?,
        dayStartTime: String?,
        datEndTime: String?
    ) {
        if (holidays != null && holidays.isNotEmpty()) {
            this.holidays.clear()
            this.holidays.addAll(holidays)
        }
        if (workdays != null && workdays.size > 0) {
            this.workdays.clear()
            this.workdays.addAll(workdays)
        }
        this.dayStartTime = dayStartTime
        this.datEndTime = datEndTime
    }

    /**
     * 根据判断当前时间是否是配置中的节假日还是工作日
     */
    private val dateType: Int
        private get() {
            var type = TYPE_DAY_NONE
            val now = Date()
            val today = sdf.format(now)
            if (holidays.size > 0) {
                for (holiday in holidays) {
                    if (holiday == today) {
                        type = TYPE_DAY_HOLIDAY
                        break
                    }
                }
            }
            if (workdays.size > 0) {
                for (workday in workdays) {
                    if (workday == today) {
                        type = TYPE_DAY_WORK
                    }
                }
            }
            return type
        }

    /**
     * 是否是周末
     */
    private val isWeekend: Boolean
        get() {
            val now = Date()
            val cal = Calendar.getInstance()
            cal.time = now
            return (cal[Calendar.DAY_OF_WEEK] == Calendar.SATURDAY
                    || cal[Calendar.DAY_OF_WEEK] == Calendar.SUNDAY)
        }

    /**
     * 是否是节假日
     */
    val isHoliday: Boolean
        get() {
            return when(dateType) {
                TYPE_DAY_HOLIDAY -> true
                TYPE_DAY_WORK -> false
                else -> isWeekend
            }
        }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    private fun belongCalendar(
        nowTime: Date?, beginTime: Date?,
        endTime: Date?
    ): Boolean {
        val date = Calendar.getInstance()
        date.time = nowTime
        val begin = Calendar.getInstance()
        begin.time = beginTime
        val end = Calendar.getInstance()
        end.time = endTime
        return date.after(begin) && date.before(end)
    }

    /**
     * 判断当前时间是否在当天可玩时间段
     *
     * @return
     */
    fun betweenPlayTime(): Boolean {
        val df = SimpleDateFormat(formatTime)
        var now: Date? = null
        var beginTime: Date? = null
        var endTime: Date? = null
        try {
            now = df.parse(df.format(Date()))
            beginTime = df.parse(dayStartTime)
            endTime = df.parse(datEndTime)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return belongCalendar(now, beginTime, endTime)
    }

    /**
     * 解析时间字符串的时间，返回当前小时
     * @param time String?
     * @return Int
     */
    fun parseTime(time: String?): Int {
        val df = SimpleDateFormat(formatTime)
        var date: Date? = null
        try {
            date = df.parse(time)
        } catch (e: ParseException) {
            e.printStackTrace()
        }

        return date!!.hours
    }

    /**
     * 获取当天的日期
     */
    val todayDate: String
        get() {
            return sdf.format(Date(System.currentTimeMillis()))
        }

    companion object {
        private const val TYPE_DAY_NONE = 0
        private const val TYPE_DAY_HOLIDAY = 1
        private const val TYPE_DAY_WORK = 2

        /**
         * 返回DateManager对象
         */
        val instance: YMDateManager
            get() = SingletonHolder.INSTANCE
    }
}